package com.code;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint.Align;

import com.Global.Global;
import com.code.chart.AbstractDemoChart;

public class Chart_DoanhThuTuan extends AbstractDemoChart {

	private double iMaxY = 0;
	private int iMonthNowPlus = Calendar.getInstance().get(Calendar.MONTH) + 2;
	private int iMonthNow = Calendar.getInstance().get(Calendar.MONTH) + 1;
	private int iYearNowAbt = Calendar.getInstance().get(Calendar.YEAR) - 1;
	private int iYearNow = Calendar.getInstance().get(Calendar.YEAR);
	private int iWeek = Calendar.getInstance().get(Calendar.WEEK_OF_YEAR);
//	private int iWeekNowPlus = Calendar.getInstance().get(
//			Calendar.WEEK_OF_YEAR + 1);

	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getDesc() {
		// TODO Auto-generated method stub
		return null;
	}

	public Intent execute(Context context,String kho) {
		double dbAllDoanhThu[] = getDoanhThuByWeek(kho);
		// double dbCallHasSPInYear[] =
		// getCallInYear(" AND SoPhieu IS NOT NULL");
		String[] titles = new String[] { "Doanh thu theo tuần (VNĐ/Tuần)" };
		List<double[]> x = new ArrayList<double[]>();
		for (int i = 0; i < titles.length; i++) {
			for (int j = 0; j <= 54; j++) {
				x.add(new double[] { j + 1 });
			}
		}
		List<double[]> values = new ArrayList<double[]>();
		values.add(dbAllDoanhThu);
		// values.add(dbCallHasSPInYear);
		int[] colors = new int[] { Color.GREEN};
		XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
		// iMaxY = iMaxY * 12 / 10;
		setChartSettings(renderer, "", "Tuần", "", 0.5, 12.5, 0, iMaxY,
				Color.GRAY, Color.LTGRAY);
		renderer.getSeriesRendererAt(0).setDisplayChartValues(true);
		// renderer.getSeriesRendererAt(1).setDisplayChartValues(true);
		renderer.setXLabels(0);
		renderer.setYLabels(10);

		for (int i = iWeek ; i < iWeek + 54; i++) {
			renderer.addXTextLabel(i - iWeek + 1, returnstrWeek(i));
		} 
		renderer.setXLabelsAlign(Align.LEFT);
		renderer.setYLabelsAlign(Align.CENTER);
		renderer.setPanEnabled(true, false);
		// renderer.setZoomEnabled(false);
		renderer.setZoomRate(1.1f);
		renderer.setBarSpacing(0.5f);
		renderer.setZoomButtonsVisible(true);
		return ChartFactory.getBarChartIntent(context,
				buildBarDataset(titles, values), renderer, Type.STACKED,
				"TK doanh thu từ tuần " + String.valueOf(iWeek) + " Năm "
						+ String.valueOf(iYearNowAbt) + " đến tuần "
						+ String.valueOf(iWeek) + " Năm "
						+ String.valueOf(iYearNow) );
	}


	private double[] getDoanhThuByWeek(String kho) {
		double dbThangSLGoi[] = new double[54];
		for(int i =0;i<=53;i++){
		 dbThangSLGoi[i] = 0;
		}
		StringBuffer sql = new StringBuffer(" SELECT DATEPART(WEEK,Ngay) Tuan,DATEPART(YEAR,Ngay) Nam,SUM(CASE WHEN ISNULL(Thanh_Tien,'') = '' THEN 0 ELSE Thanh_Tien END ) Tien ");
				sql.append(" FROM PhieuXuat JOIN VatTuXuat ON PhieuXuat.SoCt = VatTuXuat.SoCt");
				sql.append( " WHERE Ngay > = '"+ String.valueOf(iYearNowAbt)+ "/"+ String.valueOf(iMonthNowPlus)+ "/1"	+ "'");
				if(!kho.equals(""))
					sql.append(" AND MSK COLLATE DATABASE_DEFAULT in (select value from  dbo.fn_Split('"+kho+"', ','))");
				sql.append( " GROUP BY DATEPART(WEEK,Ngay) ,DATEPART(YEAR,Ngay)  ");
				sql.append( " ORDER BY DATEPART(YEAR,Ngay),DATEPART(WEEK,Ngay) " );
		ResultSet rs = Global.excuteQuerySQLServer(sql.toString());
		try {
			while (rs.next()) {
				int iweekIndex = Integer.parseInt(rs.getString("Tuan")
						.toString());
				double iTienIndex = Global.convertMooneyByDouble( Double.parseDouble(rs.getString("Tien")
						.toString()));
				dbThangSLGoi[returniWeek(iweekIndex - iWeek + 53) ] = (double) iTienIndex;

				dbThangSLGoi[0] = 0;
				dbThangSLGoi[53]= 0;
				if(rs.getString("Tuan").toString().equals(String.valueOf(iWeek)) && rs.getString("Nam").toString().equals(String.valueOf(iYearNowAbt)))
					dbThangSLGoi[0] =  Global.convertMooneyByDouble(Double.parseDouble(rs.getString("Tien")
							.toString()));
				if(rs.getString("Tuan").toString().equals(String.valueOf(iWeek)) && rs.getString("Nam").toString().equals(String.valueOf(iYearNow)))
					dbThangSLGoi[53] =  Global.convertMooneyByDouble(Double.parseDouble(rs.getString("Tien")
							.toString()));
				// set max Y
				if (iTienIndex > iMaxY)
					iMaxY = iTienIndex;
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return dbThangSLGoi;
	}

	public String returnstrWeek(int iMonth) {
		if (iMonth == 53 || iMonth == 0)
			return String.valueOf(53);
		else
			return String.valueOf(iMonth % 53);
	}

	public int returniWeek(int iMonth) {
		if (iMonth == 53 || iMonth == 0)
			return 53;
		else
			return iMonth % 53;
	}

	public Intent execute(Context context) {
		// TODO Auto-generated method stub
		return null;
	}

}
